package business.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import vo.Topic;
import business.TopicService;
import dao.TopicDAO;

public class TopicServiceImpl implements TopicService {

	private TopicDAO topicDao;

	public List<Topic> getAll() {
		return topicDao.findAll();
	}

	public List<Topic> getAll(int start, int pageSize) {
		return topicDao.findAll(start, pageSize);
	}

	public List<Topic> getAllByName(int start, int pageSize, String name) {
		return topicDao.findByName(start, pageSize, name);
	}

	public List<Topic> getAllByExpireDttm(int start, int pageSize, String expireDttm) {
		return topicDao.findByExpireDttm(start, pageSize, expireDttm);
	}

	public List<Topic> getAllByNameAndExpireDttm(int start, int pageSize, String name, String expireDttm) {
		return topicDao.findByNameAndExpireDttm(start, pageSize, name, expireDttm);
	}

	// get counts
	public long getCount() {
		return topicDao.findCount();
	}

	public long getCountByName(String name) {
		return topicDao.findCountByName(name);
	}

	public long getCountByExpireDttm(String expireDttm) {
		return topicDao.findCountByExpireDttm(expireDttm);
	}

	public long getCountByNameAndExpireDttm(String name, String expireDttm) {
		return topicDao.findCountByNameAndExpireDttm(name, expireDttm);
	}

	@Required
	public void setTopicDao(TopicDAO topicDao) {
		this.topicDao = topicDao;
	}

	@Transactional
	public void addTopic(String name) {
		Topic t = new Topic();
		t.setName(String.valueOf(Calendar.getInstance().getTime()));
		t.setWarnLimit((int) Calendar.getInstance().getTimeInMillis());//
		t.setInclude("include");
		topicDao.create(t);
	}

	@Transactional
	public void addTopic(Topic topic) {
		topicDao.create(topic);
	}

	@Transactional
	public Topic getById(long id) {
		return topicDao.getById(id);
	}

	@Transactional
	public void update(long id) {
		topicDao.update(id);
	}

	@Transactional
	public void update(Topic topic) {
		topicDao.update(topic);
	}

	@Transactional
	public void delete(long id) {
		// 不是真是的删除
		topicDao.delete(id);
	}

}